package com.angel.web.health;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.angel.core.Constants;
import com.angel.core.util.CodeUtil;
import com.angel.core.util.FileUpload;
import com.angel.core.util.JsonUtil;
import com.angel.core.util.Pager;
import com.angel.health.BuzCpr;
import com.angel.provider.health.BuzCprProvider;

/**
 * @author 作者姓名 cheng
 * @version 创建时间：2016年9月23日 下午6:13:33 类说明 电子病例
 */
@Controller
@RequestMapping("/cpr")
public class BuzCprController {

	@Autowired
	private BuzCprProvider provider;

	@RequestMapping("/index")
	public String index(BuzCpr cpr, HttpServletRequest request, Pager pager) {
		cpr.setEnable(1);
		Pager bean = provider.findAll(cpr, pager);
		request.setAttribute("pager", bean);
		request.setAttribute("cpr", cpr);
		return "pomr/mar_case";
	}

	/**
	 * 
	 * @param cpr
	 * @param multiFileMap
	 *            所有上传文件
	 * @return
	 */
	@RequestMapping(value = "save")
	public String save(@ModelAttribute("cpr") BuzCpr cpr, HttpServletRequest request) {
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		MultiValueMap<String, MultipartFile> multiFileMap = multipartRequest.getMultiFileMap();
		try {
			String createBy = request.getSession().getAttribute("user").toString();
			cpr.setUpdateBy(createBy);
			cpr.setCreateBy(createBy);
			String uploadUrl = FileUpload.uploadForFastdfs(multiFileMap);
			cpr.setUploadUrl(uploadUrl);
			int i = provider.insert(cpr);
			
			BuzCpr e = new BuzCpr();
			e.setCode(CodeUtil.createCode(null, Constants.CPR, cpr.getId()));
			e.setId(cpr.getId());
			provider.updateById(e);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "redirect:/cpr/index";
	}

	/**
	 * 跳转到添加页面
	 * 
	 * @return
	 */
	@RequestMapping("addPage")
	public String addPage() {
		return "pomr/mar_case_add";
	}

	/**
	 * 批量删除
	 * 
	 * @param ids
	 * @return
	 */
	@ResponseBody
	@RequestMapping("del")
	public String del(String ids) {
		int delAll = provider.delAll(ids);
		if (delAll > 0) {
			return JsonUtil.toJson(delAll);
		} else {
			return JsonUtil.toJson(0);
		}
	}

	/**
	 * 查看或者编辑页面
	 * 
	 * @param request
	 * @param id
	 *            电子病例id
	 * @param isEdit
	 *            1为可编辑 其他为查看
	 * @return
	 */
	@RequestMapping("/toLook/{id}/{isEdit}")
	public String toLook(HttpServletRequest request, @PathVariable("id") Integer id,
			@PathVariable("isEdit") String isEdit) {
		BuzCpr cpr = provider.findById(id);
		request.setAttribute("cpr", cpr);
		request.setAttribute("isEdit", isEdit);
		return "pomr/mar_case_edit";
	}

	/**
	 * 修改信息
	 * @param cpr
	 * @return
	 */
	@RequestMapping("/edit")
	public String update(HttpServletRequest request,BuzCpr cpr) {
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		MultiValueMap<String, MultipartFile> multiFileMap = multipartRequest.getMultiFileMap();
		String updateBy = request.getSession().getAttribute("user").toString();
		String uploadUrl;
		try {
			uploadUrl = FileUpload.uploadForFastdfs(multiFileMap);
			cpr.setUploadUrl(uploadUrl);
			cpr.setUpdateBy(updateBy);
			int i = provider.updateById(cpr);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "redirect:/cpr/index";
	}
}
